package org.example.new_hbps_backend_reconstruction.controller.patient;

/**
 * @author 丁利军
 * @date 2025年10月15日 21:52
 * @description 血氧相关接口
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.new_hbps_backend_reconstruction.entity.BloodOxygenLogsEntity;
import org.example.new_hbps_backend_reconstruction.service.BloodOxygenLogsService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/patient/blood-oxygen")
@Tag(name = "血氧数据管理", description = "提供血氧数据的查询、统计、分析接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@Slf4j
public class BloodOxygenLogsController {

    private final BloodOxygenLogsService bloodOxygenLogsService;

    @Operation(summary = "查询最新一条血氧数据", description = "根据 patientUid 查询最新的血氧记录")
    @Parameter(name = "patientUid", description = "患者 UID", required = true)
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到记录")
    })
    @GetMapping("/latest/{patientUid}")
    public ResponseEntity<BloodOxygenLogsEntity> getLatest(@PathVariable Long patientUid) {
        log.info("查询 patientUid={} 的最新血氧记录", patientUid);
        return ResponseEntity.ok(bloodOxygenLogsService.getLatestByPatientUid(patientUid));
    }

    @Operation(summary = "分页查询血氧数据", description = "根据 patientUid 分页查询血氧记录")
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "pageNum", description = "页码", required = false),
            @Parameter(name = "pageSize", description = "每页大小", required = false)
    })
    @GetMapping("/page/{patientUid}")
    public ResponseEntity<IPage<BloodOxygenLogsEntity>> getPage(
            @PathVariable Long patientUid,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(bloodOxygenLogsService.getPageByPatientUid(patientUid, pageNum, pageSize));
    }

    @Operation(summary = "按日期查询血氧数据", description = "根据 patientUid 和日期查询血氧记录")
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "date", description = "日期 yyyy-MM-dd", required = true)
    })
    @GetMapping("/by-date/{patientUid}")
    public ResponseEntity<List<BloodOxygenLogsEntity>> getByDate(
            @PathVariable Long patientUid,
            @RequestParam String date) {
        LocalDate localDate = LocalDate.parse(date);
        return ResponseEntity.ok(bloodOxygenLogsService.getByPatientUidAndDate(patientUid, localDate));
    }

    @Operation(summary = "查询所有血氧数据", description = "根据 patientUid 查询所有血氧记录")
    @Parameter(name = "patientUid", description = "患者 UID", required = true)
    @GetMapping("/all/{patientUid}")
    public ResponseEntity<List<BloodOxygenLogsEntity>> getAll(@PathVariable Long patientUid) {
        return ResponseEntity.ok(bloodOxygenLogsService.getAllByPatientUid(patientUid));
    }

    @Operation(summary = "统计血氧记录数量", description = "统计某个日期下患者的血氧记录条数")
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "date", description = "日期 yyyy-MM-dd", required = true)
    })
    @GetMapping("/count/{patientUid}")
    public ResponseEntity<Long> count(
            @PathVariable Long patientUid,
            @RequestParam String date) {
        LocalDate localDate = LocalDate.parse(date);
        return ResponseEntity.ok(bloodOxygenLogsService.countByPatientUidAndDate(patientUid, localDate));
    }

    @Operation(summary = "计算平均血氧值", description = "计算某个日期下患者的平均血氧值")
    @Parameters({
            @Parameter(name = "patientUid", description = "患者 UID", required = true),
            @Parameter(name = "date", description = "日期 yyyy-MM-dd", required = true)
    })
    @GetMapping("/avg/{patientUid}")
    public ResponseEntity<Double> averageSpo2(
            @PathVariable Long patientUid,
            @RequestParam String date) {
        LocalDate localDate = LocalDate.parse(date);
        return ResponseEntity.ok(bloodOxygenLogsService.averageSpo2ByPatientUidAndDate(patientUid, localDate));
    }
}
